public class Sort {
    public static void insertSort(int[] array){
        for(int i = 1; i < array.length;i++){
            int tmp = array[i];
            int j = i - 1;
            for(;j >= 0; j--){
                if(array[j]>tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
        // write code  here
    }

    // 希尔排序
    public static void shell(int[] array,int gap){
        for(int i = gap; i < array.length;i++){
            int tmp = array[i];
            int j = i - gap;
            for(;j >= 0;j-=gap){
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap > 1){
            gap/=2;
            shell(array,gap);
        }
        // write code  here
    }

    // 选择排序
    public static void selectSort(int[] array){
        for(int i = 0; i < array.length; i++){
            int minIndex = i;
            for(int j = i+1; j < array.length; j++){
                if(array[j] < minIndex){
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
        // write code  here
    }
    public static void selectSort2(int[] array){
        int left = 0;
        int right = array.length-1;
        while(left < right){
            int maxIndex = left;
            int minIndex = left;
            for(int i = left+ 1; i <= right; i++){
                if(array[i] > array[maxIndex]){
                    minIndex = i;
                }
                if(array[i] < array[minIndex]){
                    minIndex = i;
                }
            }
            swap(array,minIndex,left);
            if(maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }

    public static void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }



// 堆排序
    public static void heapSort(int[] array){
        createBigHeap(array);
        int end = array.length-1;
        while(end > 0) {
            swap(array, 0, end);
            end--;
        }
    }
    public static void createBigHeap(int[] array){
        for(int parent = (array.length-1-1)/2; parent >= 0;parent--){
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[] array,int parent ,int end){
        int child = 2*parent+1;
        while(child < end){
            if(child + 1 < end && array[child]<array[child+1]){
                child++;
            }
            if (array[child] > array[parent]){
             swap(array,child,parent);
             parent = child;
             child = 2*parent+1;
            }else{
                break;
            }
        }
    }
    // 冒泡排序
    public static void bubbleSort(int[] array){
        for(int i = 0; i< array.length-1;i++){
            boolean flg = false;
            for(int j = 0; j < array.length-1-i; j++){
                if(array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if(flg = false){
                break;
            }
        }
    }

// 快速
private static void quickSortHelper(int[] array, int left, int right) {
    if (left < right) {
        // 进行分区操作，获取基准元素的最终位置
        int pivotIndex = partition(array, left, right);
        // 递归对基准元素左边的子数组进行排序
        quickSortHelper(array, left, pivotIndex - 1);
        // 递归对基准元素右边的子数组进行排序
        quickSortHelper(array, pivotIndex + 1, right);
    }
}
    public static int partition(int[] array,int left,int right){
        int key = array[left];
        while (left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= key) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }

}
